﻿using DoNetDrive.Common;
using DoNetDrive.Common.Cryptography;
using DoNetDrive.Common.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

namespace DoNetTool.Common.Core.Web
{
    /// <summary>
    /// 参数签名工具类
    /// </summary>
    public class SignatureTool
    {
        /// <summary>
        /// 生成带有签名的字典对象
        /// </summary>
        /// <param name="oParDetail">类对象</param>
        /// <param name="AppSecret">密钥</param>
        /// <param name="AppKey">键值</param>
        /// <returns></returns>
        public static SortedDictionary<string, string> CreateSignature(object oParDetail, string AppSecret, string AppKey)
        {
            if (string.IsNullOrWhiteSpace(AppSecret) || string.IsNullOrWhiteSpace(AppKey))
            {
                return null;
            }
            string sJson = JsonSerializer.Serialize(oParDetail);
            Dictionary<string, object> oDic = JsonSerializer.Deserialize<Dictionary<string, object>>(sJson);
            if (!oDic.ContainsKey("AppKey"))
            {
                oDic.Add("AppKey", AppKey);
            }
            else
            {
                if (oDic["AppKey"] == null)
                {
                    oDic["AppKey"] = AppKey;
                }
                if (string.IsNullOrEmpty(oDic["AppKey"].ToString()))
                {
                    oDic["AppKey"] = AppKey;
                }
            }

            if (!oDic.ContainsKey("Timestamp"))
            {
                string sTimestamp = DateTime.Now.ToDateTimeStr();
                oDic.Add("Timestamp", sTimestamp);
            }
            else
            {
                if (oDic["Timestamp"] == null)
                {
                    oDic["Timestamp"] = DateTime.Now.ToDateTimeStr();
                }

                if (string.IsNullOrEmpty(oDic["Timestamp"].ToString()))
                {
                    oDic["Timestamp"] = DateTime.Now.ToDateTimeStr();
                }
            }

            SortedDictionary<string, string> par = new SortedDictionary<string, string>();
            foreach (KeyValuePair<string, object> item in oDic)
            {
                if (item.Value != null && !string.IsNullOrEmpty(item.Value.ToString()))
                {
                    if (!item.Key.Equals("Signature"))
                    {
                        if (item.Value != null)
                        {
                            par.Add(item.Key, item.Value.ToString());
                        }

                    }

                }
            }
            par.Add("Signature", CreateSignatureHash(par, AppSecret, AppKey));
            return par;
        }

        /// <summary>
        /// 参数签名算法
        /// 根据参数列表做签名校验
        /// 参数列表需要按参数名排序
        /// Signature 本身不参与排序
        /// </summary>
        /// <param name="par"></param>
        /// <param name="AppSecret"></param>
        /// <param name="AppKey"></param>
        /// <returns></returns>
        public static string CreateSignatureHash(SortedDictionary<string, string> par, string AppSecret, string AppKey)
        {
            StringBuilder sSignature = new StringBuilder(1000);
            sSignature.Clear();
            foreach (KeyValuePair<string, string> item in par)
            {
                sSignature.Append(item.Key);
                sSignature.Append("=");
                sSignature.Append(item.Value);
                sSignature.Append("&");
            }
            sSignature.Length -= 1;

            string signature = sSignature.ToString();
            string temp = MyAES.Encryptor(signature, AppSecret, AppKey);

            var data = EncryptTool.MD5StrToBty(temp);
            return data.ToHex();
        }


    }
}
